//
//  ZQTabBar.swift
//  ZQWeibo
//
//  Created by summer的Dad on 16/11/12.
//  Copyright © 2016年 summer的Dad. All rights reserved.
//

import UIKit

class ZQTabBar: UITabBar {

    var composeClosure : (() -> ())?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        
        self.addSubview(composeBtn)
        
        //用闭包监听按钮点击事件
        
        self.composeBtn.addTarget(self , action: #selector(clickBtn), for: .touchUpInside)
        
        
        
    }
    //点击事件
      @objc private func clickBtn () {
    
        
        composeClosure? ()
        
    }
 
    //用XIB会崩掉
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
//        super.init(coder: aDecoder)
//        self.addSubview(composeBtn)
    
    
    }
    
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        //定义宽高
        
        let width = UIScreen.main.bounds.width / 5
        
        let heigth = self.bounds.height
        //tag
        var index = 0
        
        
        for subview in subviews {
            //判断拿到UItabbarIrem
            if subview.isKind(of: NSClassFromString("UITabBarButton")!) {
                //布局控件
                subview.frame = CGRect(x: CGFloat(index) * width, y: 0, width: width, height: heigth)
                
                
                index += (index == 1 ? 2 : 1)
            }
            
            
            
        }
        //设置懒加载中间按钮
        
        composeBtn.bounds.size = CGSize(width: width, height: heigth)
        
        
        composeBtn.center = CGPoint(x: self.center.x, y: heigth * 0.5)
        
        
    
    }
    //懒加载 中间的控件
    
    lazy var composeBtn : UIButton = {
        
        let btn = UIButton()
        
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add"), for: .normal)
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add_highlighted"), for: .selected)
        
        
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button"), for: .normal)
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button_highlighted"), for: .selected)
        
        
        return btn
    }()
    
    
    
    
    
    
    
    
    
}
